package brwarner.radial 
{
	import flash.display.Sprite;
	import aze.motion.*;
	
	/**
	 * ...
	 * @author Brook Jensen
	 */
	public class Menu extends Sprite
	{
		//Menu items
		private var _items:Array;
		
		//Radius of the menu
		private var _radius:Number;
		
		public function Menu() 
		{
			_items = new Array();
			_radius = 75;
		}
		
		public function clearItems( ):void
		{
			_items.forEach( function( e:Item, index:Number, a:Array ):void {
				removeChild(e);
			}, this);
			_items.splice( 0 );
		}
		
		public function set radius( r:Number ) : void
		{
			_radius = r;
			position();
		}
		public function get radius( ) : Number
		{
			return _radius;
		}
		
		//Plays the open animation to the default radius
		public function open( ) : void
		{
			radius = 0;
			visible = true;
			eaze( this ).to( 0.1, { radius: 40 } );
		}
		
		public function close( ):void
		{
			visible = false;
			radius = 0;
		}
		
		//Organizes the items in the menu
		private function position( ):void
		{
			//Angle per item constant
			var anglePerItem:Number = 2 * Math.PI / _items.length;
			
			//For each item
			for ( var i:Number = 0; i < _items.length; i++ )
			{
				//Get the item
				var item:Item = _items[i] as Item;
				
				//Calculate angle
				var angle:Number = anglePerItem * i;
				
				//Position using trig
				item.x = Math.cos( angle ) * _radius;
				item.y = Math.sin( angle ) * _radius;
			}
		}
		
		public function addItem( item:Item ) : void
		{
			//Add it as a child
			addChild(item); _items.push(item);
		}
		
	}

}